home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-04-25 | 7.0 KB | 282 lines | [TEXT/CWIE] |
- // ==================================================
- // CAppleGuideFile.cp
- // Copyright (C) 1997 Mizutori Tetsuya
- // March 21, 1997.
- // ==================================================
- // All documents are pretty-printed in 10-point Geneva font.
-
- #include <AppleGuide.h>
- #include <TextUtils.h>
-
- #include <LString.h>
- #include <UEnvironment.h>
- #include <UDesktop.h>
-
- #include "AGFile.h"
-
- #include "CAppleGuideFile.h"
- #include "UFileTools.h"
- #include "UErrorMessage.h"
-
-
- const OSType kOSTypeNil = (Int32) -1;
-
-
- // --------------------------------------------------
- // • CAppleGuideFile
- // --------------------------------------------------
-
- CAppleGuideFile::CAppleGuideFile()
- {
- mGotGuideFile = false;
- mGuideCreatorType = kOSTypeNil;
- mGuideFileRefNum = 0;
- mGuideFilename[0] = 0;
- }
-
-
- // --------------------------------------------------
- // • CAppleGuideFile()
- // --------------------------------------------------
-
- CAppleGuideFile::CAppleGuideFile(
- ConstStr255Param inFileName )
- {
- mGotGuideFile = false;
- mGuideCreatorType = kOSTypeNil;
- mGuideFileRefNum = 0;
- LString::CopyPStr( inFileName, mGuideFilename );
- }
-
-
- // --------------------------------------------------
- // • CAppleGuideFile()
- // --------------------------------------------------
-
- CAppleGuideFile::CAppleGuideFile(
- OSType inCreatorType )
- {
- mGotGuideFile = false;
- mGuideCreatorType = (OSType) inCreatorType;
- mGuideFileRefNum = 0;
- mGuideFilename[0] = 0;
- }
-
-
- // --------------------------------------------------
- // • ~CAppleGuideFile
- // --------------------------------------------------
-
- CAppleGuideFile::~CAppleGuideFile()
- {
- if ( ! AppleGuideIsPresent() ) return;
-
- AGErr err;
- // kAGIsActive(open), kAGIsSleeping(running but not open), kAGIsNotRunning(not running)
- AGStatus agStatus = ::AGGetStatus();
-
- switch ( agStatus ) {
- case kAGIsNotRunning:
- // Apple Guide is not running. So we do nothing to exit.
- break;
-
- case kAGIsSleeping:
- // Apple Guide is running, but no guide file is open.
- err = ::AGQuit();
- break;
-
- case kAGIsActive:
- // Apple Guide is running, and some guide file is open.
- if ( ! ::AGIsDatabaseOpen( mGuideFileRefNum ) ) break;
- // AGClose returns kAGErrInvalidRefNum if the guide file was opened by another application.
- if ( ::AGClose( &mGuideFileRefNum ) != noErr ) break;
- err = ::AGQuit();
- break;
-
- default:
- break;
- }
- }
-
-
- // ==================================================
- // Member functions
- // ==================================================
-
- // --------------------------------------------------
- // • AppleGuideIsPresent
- // --------------------------------------------------
- // Check if Apple Guide is installed or not.
-
- Boolean
- CAppleGuideFile::AppleGuideIsPresent( void )
- {
- //check if the Apple Guide extension is installed.
- return UEnvironment::HasGestaltAttribute( gestaltHelpMgrAttr, gestaltAppleGuidePresent );
- }
-
-
- // --------------------------------------------------
- // • Open
- // --------------------------------------------------
- // Open the specified guide file located in the application's folder.
-
- AGErr
- CAppleGuideFile::Open( void )
- {
- AGErr err;
-
- if ( ! AppleGuideIsPresent() ) {
- err = kAGErrAppleGuideNotAvailable;
- UErrorMessage::NofityIfOSErr( err, false );
- return err;
- }
-
- // Check if Apple Guide file is already open or not.
- // kAGIsActive(open), kAGIsSleeping(running but not open), kAGIsNotRunning(not running)
- AGStatus agStatus;
- agStatus = ::AGGetStatus();
- if ( agStatus == kAGIsActive ) return kAGErrDatabaseOpen;
-
- // If a particular guide file was not found in the same folder of application, then returns err.
- // If guide file was found, the mGuideFileFSSpec will be set to its file spec.
- // err = FindGuideFile( mGuideFilename );
- err = FindGuideFile( mGuideCreatorType );
-
- if ( err != noErr ) {
- err = kAGErrDatabaseNotAvailable;
- UErrorMessage::NofityIfOSErr( err, false );
- return err;
- }
-
- // Open a guide file and show a Full Access window.
- AGRefNum agRefNum;
- err = ::AGOpen( &mGuideFileFSSpec /*kAGDefault*/, 0, NULL, &agRefNum );
- if ( err != noErr ) return err;
-
- mGuideFileRefNum = agRefNum;
-
- return err;
- }
-
-
- // --------------------------------------------------
- // • FindGuideFile
- // --------------------------------------------------
- // Find a default guide file in the same folder where the application is located.
- // The file type of default guide file is kAGFileMain('poco').
-
- OSErr
- CAppleGuideFile::FindGuideFile(
- ConstStr255Param inFileName )
- {
- OSErr err;
-
- FSSpec theApplFSSPec;
- err = UFileTools::GetCurrentApplicationFile( theApplFSSPec );
- if ( err != noErr ) return err;
-
- Boolean found = false;
- for ( long index = 1; ! found ; index++ ) {
-
- FSSpec theFSSpec;
- EFileType theFileType;
- err = UFileTools::SearchDirectory( index, theApplFSSPec.vRefNum, theApplFSSPec.parID,
- theFSSpec, theFileType );
- if ( err != noErr ) break;
-
- // If it is a directory (folder or volume), then skip to next item.
- if ( theFileType != fileType_File ) continue;
-
- // Now we get a plain file, and check its file name if inFileName is not NULL.
- if ( inFileName[0] > 0 &&
- // ::CompareString( theFSSpec.name, inFileName, NULL ) != 0 ) {
- ! ::EqualString( theFSSpec.name, inFileName, false, false ) ) {
- continue;
- }
-
- // Now we get a plain file, and check its file type.
- FInfo theFinderInfo;
- err = ::FSpGetFInfo( &theFSSpec, &theFinderInfo );
- if ( err != noErr ) break;
-
- if ( theFinderInfo.fdType == kAGFileMain ) {
- // Now we have got a guide file, whose file type is a kAGFileMain('poco').
- mGotGuideFile = true;
- mGuideFileFSSpec = theFSSpec;
- err = noErr;
- found = true;
- }
- }
-
- return err;
- }
-
-
- OSErr
- CAppleGuideFile::FindGuideFile(
- OSType inAppCreatorType )
- {
- OSErr err;
-
- FSSpec theApplFSSPec;
- err = UFileTools::GetCurrentApplicationFile( theApplFSSPec );
- if ( err != noErr ) return err;
-
- // long count = AGFileGetDBCount( theApplFSSPec.vRefNum, theApplFSSPec.parID,
- // kAGFileDBTypeAny, false );
- Boolean found = false;
- for ( long index = 1; ! found /*&& index <= count*/; index++ ) {
-
- FSSpec theFSSpec;
- err = AGFileGetIndDB( theApplFSSPec.vRefNum, theApplFSSPec.parID,
- kAGFileDBTypeAny, false, index, &theFSSpec );
- if ( err != noErr ) break;
-
- // Check if the creator type of the guide file is a specified one.
- OSType theCreatorType;
- err = AGFileGetHelpMenuAppCreator( &theFSSpec, &theCreatorType );
- if ( err != noErr ) continue;
-
- if ( inAppCreatorType != kOSTypeNil &&
- inAppCreatorType != theCreatorType ) continue;
-
- // The found guide file is a desired one.
- mGotGuideFile = true;
- mGuideFileFSSpec = theFSSpec;
- err = noErr;
- found = true;
- }
-
- return err;
- }
-
-
- #ifdef COMMENT
- OSErr
- CAppleGuideFile::FindGuideFile(
- ConstStringPtr inFileName )
- {
- #pragma unused ( inFileName )
-
- // Deactivate the desktop.
- UDesktop::Deactivate();
-
- // Browse for a document.
- SFTypeList theTypeList = {kAGFileMain, kAGFileMixin}; // 'poco', 'mixn'
- StandardFileReply theReply;
- ::StandardGetFile( NULL, 2, theTypeList, &theReply );
- if (! theReply.sfGood ) return fnfErr;
-
- mGuideFileFSSpec = theReply.sfFile;
-
- // Activate the desktop.
- UDesktop::Activate();
-
- return noErr;
- }
- #endif // COMMENT
-
-
- // end of program
-